package com.grass.p019lv.utils;

import com.androidx.p013lv.base.utils.ImageCryptUtils;
import com.androidx.p013lv.base.utils.UiBuildConfig;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/* renamed from: com.grass.lv.utils.ReadTxtUtil */
public class ReadTxtUtil {
    public static String readNormalFile(String str) {
        try {
            File file = new File(str);
            if (!file.exists()) {
                return "";
            }
            byte[] bArr = new byte[((int) file.length())];
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(bArr);
            String str2 = new String(bArr, "UTF-8");
            fileInputStream.close();
            return str2;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String readFile(String str) {
        try {
            File file = new File(str);
            if (!file.exists()) {
                return "";
            }
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bArr = new byte[100];
            int read = fileInputStream.read(bArr);
            while (read < 100 && read != -1) {
                int read2 = fileInputStream.read(bArr, read, 100 - read);
                if (read2 == -1) {
                    return "";
                }
                read += read2;
            }
            byte[] xorEncode = ImageCryptUtils.xorEncode(bArr, 100, UiBuildConfig.ENCRYPT_TXT_KEY);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byteArrayOutputStream.write(xorEncode, 0, 100);
            while (true) {
                int read3 = fileInputStream.read(xorEncode);
                if (read3 != -1) {
                    byteArrayOutputStream.write(xorEncode, 0, read3);
                } else {
                    String str2 = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
                    fileInputStream.close();
                    return str2;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String readTxt(String str) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);
            InputStream inputStream = httpURLConnection.getInputStream();
            byte[] bArr = new byte[100];
            int read = inputStream.read(bArr);
            while (read < 100 && read != -1) {
                int read2 = inputStream.read(bArr, read, 100 - read);
                if (read2 == -1) {
                    return "";
                }
                read += read2;
            }
            byte[] xorEncode = ImageCryptUtils.xorEncode(bArr, 100, UiBuildConfig.ENCRYPT_TXT_KEY);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byteArrayOutputStream.write(xorEncode, 0, 100);
            while (true) {
                int read3 = inputStream.read(xorEncode);
                if (read3 == -1) {
                    return new String(byteArrayOutputStream.toByteArray());
                }
                byteArrayOutputStream.write(xorEncode, 0, read3);
            }
        } catch (Exception unused) {
            return "加载出错";
        }
    }
}
